@using Bit.SharedWeb.Utilities
@using Bit.Admin.Enums;
@inject Bit.Admin.Services.IAccessControlService AccessControlService

@model ProvidersModel
@{
    ViewData["Title"] = "Providers";

    var canCreateProvider = AccessControlService.UserHasPermission(Permission.Provider_Create);
}

<h1>Providers</h1>

<form class="row row-cols-lg-auto g-3 align-items-center mb-2" method="get">
    <div class="col-12">
        <label class="visually-hidden" asp-for="Name">Name</label>
        <input type="text" class="form-control" placeholder="Name" asp-for="Name" name="name">
    </div>
    <div class="col-12">
        <label class="visually-hidden" asp-for="UserEmail">User email</label>
        <input type="text" class="form-control" placeholder="User email" asp-for="UserEmail" name="userEmail">
    </div>
    <div class="col-12">
        <button type="submit" class="btn btn-primary" title="Search">
            <i class="fa fa-search"></i> Search
        </button>
    </div>
    @if (canCreateProvider)
    {
        <div class="col-auto ms-auto">
            <a asp-action="Create" class="btn btn-secondary">Create Provider</a>
        </div>
    }
</form>

<div class="table-responsive">
    <table class="table table-striped table-hover">
        <thead>
            <tr>
                <th>Name</th>
                <th style="width: 190px;">Provider Type</th>
                <th style="width: 190px;">Status</th>
                <th style="width: 150px;">Created</th>
            </tr>
        </thead>
        <tbody>
            @if(!Model.Items.Any())
            {
                <tr>
                    <td colspan="5">No results to list.</td>
                </tr>
            }
            else
            {
                @foreach(var provider in Model.Items)
                {
                    <tr>
                        <td>
                            <a asp-action="@Model.Action" asp-route-id="@provider.Id">@(!string.IsNullOrEmpty(provider.DisplayName()) ? provider.DisplayName() : "Pending")</a>
                        </td>
                        <td>@provider.Type.GetDisplayAttribute()?.GetShortName()</td>
                        <td>@provider.Status</td>
                        <td>
                            <span title="@provider.CreationDate.ToString()">
                                @provider.CreationDate.ToShortDateString()
                            </span>
                        </td>
                    </tr>
                }
            }
        </tbody>
    </table>
</div>

<nav>
    <ul class="pagination">
        @if(Model.PreviousPage.HasValue)
        {
            <li class="page-item">
                <a class="page-link" asp-action="Index" asp-route-page="@Model.PreviousPage.Value"
                   asp-route-count="@Model.Count" asp-route-userEmail="@Model.UserEmail"
                   asp-route-name="@Model.Name" asp-route-paid="@Model.Paid">Previous</a>
            </li>
        }
        else
        {
            <li class="page-item disabled">
                <a class="page-link" href="#" tabindex="-1">Previous</a>
            </li>
        }
        @if(Model.NextPage.HasValue)
        {
            <li class="page-item">
                <a class="page-link" asp-action="Index" asp-route-page="@Model.NextPage.Value"
                   asp-route-count="@Model.Count" asp-route-userEmail="@Model.UserEmail"
                   asp-route-name="@Model.Name" asp-route-paid="@Model.Paid">Next</a>
            </li>
        }
        else
        {
            <li class="page-item disabled">
                <a class="page-link" href="#" tabindex="-1">Next</a>
            </li>
        }
    </ul>
</nav>
